package cn.com.dycc.topic_activity.service;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.entity.Pb_Dept_PartyPart;
import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.topic_activity.dao.Pb_Activity_DeptDao;
import cn.com.dycc.topic_activity.entity.Pb_Activity_Dept;
import cn.com.dycc.topic_activity.param.Pb_Activity_DeptParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.JsonObjectResult;
import net.sf.json.JSONArray;

@Service("pb_Activity_DeptService")
@Transactional
public class Pb_Activity_DeptServiceImpl implements Pb_Activity_DeptService{

	@Resource
	private Pb_Activity_DeptDao pb_Activity_DeptDao;
	
	
	@SystemServiceLog(description=Constants.ACTIVITY_DEPT_ADD)
	public int addPb_Activity_Dept(Pb_Activity_DeptParam param, HttpServletRequest request)
			throws Pb_Activity_DeptNotFoundException {
		checkParam(param);
		StringBuilder builder=new StringBuilder();
		if(param.getActivity_id()==null||param.getActivity_id().trim().isEmpty()){
			builder.append("请选择专题活动;");
		}
		if(param.getDept_array()==null||param.getDept_array().trim().isEmpty()){
			builder.append("请选择关联的党组织;");
		}
		if(!"".equals(builder.toString())){
			throw new Pb_Activity_DeptNotFoundException(builder.toString());
		}
		pb_Activity_DeptDao.deletePb_Activity_Dept(param.getActivity_id());
		HttpSession session=request.getSession();
		String org_id=(String)session.getAttribute("org_id");
		String opt_user_id=(String)session.getAttribute("id");
		param.setState("Y");
		param.setOrg_id(org_id);
		param.setOpt_user_id(opt_user_id);
		param.setCreate_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		List<Pb_Activity_Dept> lists = getServiceList(param);
		
		int d = pb_Activity_DeptDao.addPb_Activity_Dept(lists);
			
		return d;
		
	}
	
	/**
	 * 校验 @param param @return @throws
	 */
	public void checkParam(Pb_Activity_DeptParam param) {
		if (param == null) {
			throw new Pb_Activity_DeptNotFoundException("未登录或超时,请重新登录");
		}
		
	}

	public List<Pb_Activity_Dept> getServiceList(Pb_Activity_DeptParam param) {
		List<Pb_Activity_Dept> lists = new ArrayList<Pb_Activity_Dept>();
		String dept_array=param.getDept_array();
		if(dept_array!=null&&!dept_array.trim().isEmpty()){			
			String str=dept_array.trim();			
			JSONArray json=JSONArray.fromObject(str);			
			for(int i=0;i<json.size();i++){				
				Object check=json.get(i);				
				if("null".equals(check.toString())){
					continue;
				}				
				Pb_Activity_Dept entity = new Pb_Activity_Dept();
				entity.setActivity_id(param.getActivity_id());
				entity.setDept_id(json.getString(i));				
				entity.setState(param.getState());
				entity.setOrg_id(param.getOrg_id());
				if(param.getCreate_date()!=null&&!param.getCreate_date().trim().isEmpty()){
					entity.setCreate_date(Timestamp.valueOf(param.getCreate_date()));
				}
				entity.setOpt_user_id(param.getOpt_user_id());
				lists.add(entity);
			}
		}
		
		return lists;
	}

	public JsonObjectResult findPb_Activity_DeptPartById(String activity_id, HttpServletRequest request)
			throws Pb_Activity_DeptNotFoundException {
		if(activity_id==null||activity_id.trim().isEmpty()){
			throw new Pb_Activity_DeptNotFoundException("请选择专题活动;");
		}
		
		List<Pb_Dept_PartyPart> list=pb_Activity_DeptDao.findListDeptPart(activity_id);
		return new JsonObjectResult(list);
	}
}
